<?php
/**
 * Created by PhpStorm.
 * User: 杨永安
 * Date: 2017/4/17
 * Time: 14:02
 */

namespace app\api\controller;


use app\api\model\Goods;
use app\api\model\Shop;
use think\Controller;

class Shops extends Controller
{

    function goods($id = 0, $type = 0)
    {
        if ($id) {
            $goods = new Goods();
            $list = $goods->where('ShopId', $id)->select();
            $res['msg'] = $list;
            $res['code'] = $goods->where('ShopId', $id)->count();


            $shop = db('shop')->where('Id', $id)->find();

            if ($shop) {
                $res['shop'] = $shop;
            } else {

                $res['shop'] = "商店未找到";
                $res['code'] = -2;
            }


        } else {
            $res['msg'] = "参数不合法";
            $res['code'] = -1;
        }


        if ($type) {

            foreach ($res['msg'] as $k => $v) {
                echo $v['Id'] . ". " . $v['GoodsName'] . " " . $v['Price'] . "元\n";
            }

        } else {

            return json($res);
        }

    }

    function getshop($type = 0)
    {
        $shops = new Shop();
        $list = $shops->select();

        if ($type) {

            foreach ($list as $k => $v) {
                echo $v['Id'] . ". " . $v['ShopName'] . "\n";
            }

        } else {

            return json($list);
        }

    }

    function submit_order()
    {


        if (uid()) {

            $address = db('user_address_default')->where('UserId', uid())->find();
            if ($address) {
                $address = db('user_address')->where("Id", $address['AddressId'])->find();

                if ($address) {
                    $res['address'] = $address;
                    $res['code'] = 1;
                } else {
                    $res['code'] = -2;
                    $res['msg'] = "暂无地址";
                }

            } else {

                $res['code'] = -2;
                $res['msg'] = "暂无地址";
            }


            if (input('shop_id')) {

                $shop = db('shop')->where('Id', input('shop_id'))->find();
                if ($shop) {

                    $res['shop']['code'] = 1;
                    $res['shop']['msg'] = $shop;
                } else {

                    $res['shop']['code'] = -1;
                    $res['shop']['msg'] = "商店数据找不到";
                }

            } else {
                $res['shop']['code'] = -1;
                $res['shop']['msg'] = "商店参数有误";
            }


        } else {
            $res['msg'] = "请登录";
            $res['code'] = -1;
        }
        return json($res);

    }


    function make_order()
    {


        if (uid()) {
            $fee = input('fee');
            $shopId = input('shopId');
            $msg = input('msg');
            $cart = input('cart/a');

            $data['ShopId'] = $shopId;
            $data['UserId'] = uid();
            $data['Fee'] = $fee;
            $data['CreateTime'] = time();
            $data['Message'] = $msg;
            $data['Address'] = db('user_address_default')->where('UserId', uid())->value('AddressId');

            $addressOk = db('user_address')->where('Id', $data['Address'])->find();

            if ($addressOk) {


                $data['Status'] = 1;
                $shop = db('shop')->where('Id', $shopId)->find();
                $data['ShipFee'] = $shop['ShipPrice'];

                $data['VerifyCode'] = rand(1000, 9999);


                if ($data['Address']) {


                    $order = db('order')->insertGetId($data);


                    if ($order) {

                        $total = $fee + $data['ShipFee'];
                        foreach ($cart as $i => $v) {

                            $price = db('goods')->where('Id', $v['GoodsId'])->value("Price");
                            $total += $price * $v['Number'];
                            if ($price) {
                                $data2['OrderId'] = $order;
                                $data2['GoodsId'] = $v['GoodsId'];
                                $data2['Number'] = $v['Number'];
                                $data2['Price'] = $price;

                                $order_goods = db('order_goods')->insert($data2);
                            }

                        }


                        $res['msg'] = "下单成功,总价" . $total;
                        $res['code'] = 1;
                        $res['total'] = $total;


                        $content['content'] = "新店铺订单";
                        $content['extra'] = "NEW_MASTER";

                        publishPrivate($shop['Master'], $content, uid());


                    } else {

                        $res['msg'] = "下单失败,错误码-3";
                        $res['code'] = -3;
                    }


                } else {

                    $res['msg'] = "收货地址错误";
                    $res['code'] = -2;
                }
            } else {
                $res['msg'] = "没有收货地址";
                $res['code'] = -4;
            }

        } else {
            $res['msg'] = "请登录";
            $res['code'] = -1;
        }

        return json($res);

    }

}